package com.shty.wd.utils.module.Singapore;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import com.shty.wd.utils.module.IOTools.IOUtil;
import com.shty.wd.utils.module.related.Entity;
import com.shty.wd.utils.module.related.JsonParse;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;


public class test {

	public static void main(String[] args) throws Exception {
		if(RelationAnalysis.DIC_LOCATION.length()==0){
			RelationAnalysis.loadLocation();
		}
		if(RelationAnalysis.DIC_NAME.length()==0){
			RelationAnalysis.loadNameDic(); 
		}
		if(RelationAnalysis.DIC_ORG.length()==0){
			RelationAnalysis.loadORG();
		}
		
		//File dir = new File("C:/Users/fox/Desktop/�滪/json");
		File dir = new File("C:/Users/zhaochen/Desktop/data/facebook");
		//File dir = new File("C:/Users/fox/Desktop/�滪/test");
		JsonParse jp = new JsonParse();

		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:/Users/zhaochen/Desktop/2017 1104 新需求/2017 1104 新需求/a.txt"), "UTF-8");
		
		for(File file:dir.listFiles()) {
			String data = IOUtil.readDoc(file) ;
			JSONObject object = JSONObject.fromObject(data);	
			Entity entity =  jp.getEntity(object) ;
			RelationAnalysis.CheckRelation(entity);
			
			JSONStringer js = new JSONStringer() ;
			String idKey = null;
			String id = null;
			String url = null;
			String urlKey = null;
			if(entity.getResult().containsKey("fbid")) {
				idKey = "fbid";
				urlKey = "fburl";
				
			} else {
				idKey = "lnkid";
				urlKey = "lnkurl";
			}
			id = entity.getResult().get(idKey);
			url = entity.getResult().get(urlKey);
			
			String name = entity.getResult().get("fmt_name");
			String headline = entity.getResult().get("fmt_headline");
			boolean bRelation = entity.getHasRelations();
			String basis = entity.getBasis();
			
			if(entity.getResult().containsKey("fbid")) {
				js.object().key(idKey).value(id)/*.key(urlKey).value(url)*/.key("fmt_headline").value(headline).
				key("fmt_name").value(name).key("hasRelations").value(bRelation).key("basis").value(basis)
				.endObject();
			} else {
				String location = entity.getResult().get("fmt_location");
				js.object().key(idKey).value(id)/*.key(urlKey).value(url)*/.key("fmt_headline").value(headline).
				key("fmt_name").value(name).key("fmt_location").value(location)
				.key("hasRelations").value(bRelation).key("basis").value(basis)
				.endObject();
			}
			
			osw.write(js.toString()+"\n");
		}
		
		osw.close();

	}
	

}
